--[[

LysGuildNote

This is an updated version of GuildNote specifically written to address a few minor updates suggested by users.
]]

-- Function: Do the initial program load stuff.
function LGN_OnLoad(frame)
	frame:RegisterEvent("ADDON_LOADED");
	frame:RegisterEvent("PLAYER_ENTERING_WORLD");
	frame:RegisterEvent("PLAYER_LEAVING_WORLD");

	-- Register our slash commands
	SLASH_LGN1 = LGN_SLASH1;
	SLASH_LGN2 = LGN_SLASH2;
	SlashCmdList["LGN"] = function(msg)
		LGN_SlashCommandHandler(msg);
	end
end

-- Function: Slash command handler
function LGN_SlashCommandHandler(msg)
	msg = string.lower(msg);

	if (msg == '') then
		msg = nil;
	end

	if (msg) then
		-- Config Screen
		if (msg == LGN_CMD_ENABLE) then
			LGN_Config.Enable = true;
			return;
		elseif (msg == LGN_CMD_DISABLE) then
			LGN_Config.Enable = false;
			return;
		
		elseif (msg == LGN_CMD_SHORT) then
			if (LGN_Config.ShortNames) then
				LGN_Config.ShortNames = false;
				DEFAULT_CHAT_FRAME:AddMessage(LGN_MSG_SHORTOFF);
			else
				LGN_Config.ShortNames = true;
				DEFAULT_CHAT_FRAME:AddMessage(LGN_MSG_SHORTON);
			end
			return;

		end
	end
end

-- Function: Call this on events.
function LGN_OnEvent(frame, event, ...)
	if ((event == "ADDON_LOADED") and (select(1, ...) == "LysGuildNote")) then
		LGN_DefineData();
		if (LYS_VERS_NUM ~= nil) then
			LYS_ProgramLoadedText(LGN_Version);			
		end
		return;

	elseif (event == "PLAYER_ENTERING_WORLD") then
		frame:RegisterEvent("CHAT_MSG_SKILL");
		frame:RegisterEvent("TRADE_SKILL_CLOSE");
		return;

	elseif (event == "PLAYER_LEAVING_WORLD") then
		frame:UnregisterEvent("CHAT_MSG_SKILL");
		frame:UnregisterEvent("TRADE_SKILL_CLOSE");
		return;

	elseif ((event == "CHAT_MSG_SKILL") or (event == "TRADE_SKILL_CLOSE")) then
		if (LGN_Config.Enable) then
			if (IsInGuild()) then
				LGN_UpdateGuildNote();
			end
		end
		return;

	end
end

-- Function: Prepare data.
function LGN_DefineData()
	if (LGN_Config == nil) then
		LGN_Config = {};
	end

	if (LGN_Config.Enable == nil) then
		LGN_Config.Enable = true;
	end

	if (LGN_Config.ShortNames == nil) then
		LGN_Config.ShortNames = false;
	end
end

-- Function: Do the guild note update.
function LGN_UpdateGuildNote()
	local _, name, level, class, zone, group, online, note, officernote;

	if (CanEditPublicNote() ~= 1) then
		if (LGN_editNoteMessage == 0) then
			DEFAULT_CHAT_FRAME:AddMessage(LGN_MSG_NOTE_ERROR)
			LGN_editNoteMessage = 1;
		end
		return;
	end
 
	for	guildIndex = 1, GetNumGuildMembers() 
	do
		note = ""; officernote = "";
		name, _, _, level, class, zone, group, note, officernote, online = GetGuildRosterInfo(guildIndex);

		if (name == UnitName("player")) then
			GuildRosterSetPublicNote(guildIndex, LGN_GetSkillList());
		end
	end
end

-- Function: Get short name for skill
function LGN_GetShortName(skill)
	if (LGN_NAME[skill] ~= nil) then
		return LGN_NAME[skill];
	end
	return skill;
end

-- Function: Scan for possible specialities.
function LGN_GetSpeciality(baseSkill)
	local SpecString = "None";
	local SpecList = {};

	local shortSkill = string.sub(baseSkill, 1, 3);
	local name, texture, offset, numSpells = GetSpellTabInfo(1);
	for spellIndex = 1, numSpells, 1 do 
		local spellName = GetSpellName(spellIndex, BOOKTYPE_SPELL);
		if (spellName ~= nil) then
			local _, style;
			if (shortSkill == "Lea") then
				_, _, style = string.find(spellName, LGN_SPC_Leatherworking);

			elseif (shortSkill == "Eng") then
				_, _, style = string.find(spellName, LGN_SPC_Engineering);

			elseif (shortSkill == "Bla") then
				if (string.find(spellName, LGN_SPC_Black1)) then
					style = spellName;
				elseif (string.find(spellName, LGN_SPC_Black2)) then
					style = spellName;
				end
			end

			-- Check to see if style is set, return values it true, otherwise we continue looping through the skills.
			if (style ~= nil) then
				local retMsg = format("%s %s", style, LGN_GetShortName(baseSkill));
				return retMsg;
			end
		end
	end

	-- If no speciality was found to match the baseSkill, then return the baseSkill.
	return baseSkill;
end

-- Function: Scan player information for possible skills
function LGN_GetSkillList()
	local playerNote = "";
	local playerSkill1;
	local playerSkill2;
	local playerRank1;
	local playerRank2;
	local _;

	-- WoW now includes a nifty check for these sort of things...
	local prof1, prof2, archaeology, fishing, cooking, firstAid = GetProfessions();

	--name, icon, skillLevel, maxSkillLevel, numAbilities, spelloffset, skillLine, skillModifier = GetProfessionInfo(index)
	if (prof1 ~= nil) then
		playerSkill1, _, playerRank1, _, _, _, _, _ = GetProfessionInfo(prof1)
	end

	if (prof2 ~= nil) then
		playerSkill2, _, playerRank2, _, _, _, _, _ = GetProfessionInfo(prof2)
	end

	if (playerSkill1 ~= nil) then
--		playerSkill1 = LGN_GetSpeciality(playerSkill1);
		if (LGN_Config.ShortNames) then
		playerSkill1 = LGN_GetShortName(playerSkill1);
		end

		playerNote = format("%s(%d)", playerSkill1, playerRank1);
--		DEFAULT_CHAT_FRAME:AddMessage("DEBUG: LGN: Skill One collected - "..playerNote);
	end
	
	if (playerSkill2 ~= nil) then
--		playerSkill2 = LGN_GetSpeciality(playerSkill2);
		if (LGN_Config.ShortNames) then
			playerSkill2 = LGN_GetShortName(playerSkill2);
		end
		playerNote = format("%s+%s(%d)", playerNote, playerSkill2, playerRank2);
--		DEFAULT_CHAT_FRAME:AddMessage("DEBUG: LGN: Skill Two collected - "..playerNote);
	end

	if (string.len(playerNote) > 31) then
		playerNote = string.sub(playerNote,1,string.find(playerNote,")"));
--		DEFAULT_CHAT_FRAME:AddMessage("DEBUG: LGN: Note too long"..playerNote);
	end

	if (playerNote ~= "") then
		return playerNote;
	else
		return LGN_MSG_NOTRADESKILLS;
	end

end
